﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AyFrame.DbEntity
{
    public static class ColumnExtension
    {
        public static void StringKey(this EntityColumnInfo column, DbType dbType)
        {
            switch (dbType)
            {
                case DbType.SqlServer:
                    column.IsPrimarykey = true;
                    column.DataType = "nvarchar(50)";
                    column.DefaultValue = "newid()";
                    break;
                case DbType.MySql:
                    column.IsPrimarykey = true;
                    column.DataType = "varchar(50)";
                    break;
                case DbType.Oracle:
                    column.IsPrimarykey = true;
                    column.DataType = "nvarchar2(50)";
                    column.DefaultValue = "sys_guid()";
                    break;
            }
        }

        public static void StringLength(this EntityColumnInfo column, DbType dbType, int length)
        {
            column.IsNullable = true;

            switch (dbType)
            {
                case DbType.SqlServer:
                    column.DataType = length == -1 ? "nvarchar(max)" : $"nvarchar({length})";
                    break;
                case DbType.MySql:
                    column.DataType = length == -1 ? "text" : $"varchar({length})";
                    break;
                case DbType.Oracle:
                    column.DataType = length == -1 ? "nvarchar2(max)" : $"nvarchar2({length})";
                    break;
            }
        }

        public static void StringJson(this EntityColumnInfo column, DbType dbType)
        {
            column.IsNullable = true;
            column.IsJson = true;

            switch (dbType)
            {
                case DbType.SqlServer:
                    column.DataType = "nvarchar(max)";
                    break;
                case DbType.MySql:
                    column.DataType = "text";
                    break;
                case DbType.Oracle:
                    column.DataType = "nvarchar2(max)";
                    break;
            }
        }

        public static void DecimalDigit(this EntityColumnInfo column, DbType dbType, int digit)
        {
            column.DecimalDigits = digit;
            switch (dbType)
            {
                case DbType.SqlServer:
                    column.DataType = "decimal";
                    break;
                case DbType.MySql:
                    column.DataType = "decimal";
                    break;
                case DbType.Oracle:
                    column.DataType = "decimal";
                    break;
            }
        }
    }
}
